/*
 * $Id: ErrorCode.java,v 1.2 2009/05/18 19:25:34 afaichun Exp $
 * 
 * Copyright (C) 2008 General Electric Company. All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of the General
 * Electric Company (GE). You shall not disclose this software and shall use it
 * only in accordance with the terms of the license agreement you entered into
 * with GE.
 * 
 * GE MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
 * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR
 * NON-INFRINGEMENT. GE SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
 * AS A RESULT OF USING, MODIFYING, OR DISTRIBUTING THIS SOFTWARE OR ITS
 * DERIVATIVES.
 */
package com.ge.healthcare.autosc.onwatch.pojo;

import java.io.Serializable;
import java.util.ArrayList;

import org.springframework.validation.ObjectError;

import com.ge.healthcare.autosc.common.ASCLogger;
import com.ge.healthcare.autosc.common.database.dao.ICodeDetailsDAO;
import com.ge.healthcare.autosc.common.database.pojo.CfgCodeDetails;

public class ErrorCode implements Serializable {
	
	public static final long serialVersionUID = 0L;
	private ArrayList<String> codeList = null;
	private ArrayList<Object> arguementList = null;
	private String defaultMessage = null;
	private String objectName = null;
	
	public ErrorCode(String objectName) {
		codeList = new ArrayList<String>();
		arguementList = new ArrayList<Object>();
		this.objectName = objectName;
	}
	
	public void addCode(String code) {
		ASCLogger.debug(this.getClass(), "addCode", "Add messageSource code: " + code);
		codeList.add(code);
	}
	
	public void addArgument(Object argument) {
		arguementList.add(argument);
	}
	
	public void AddErrorCodeAsArgument(ICodeDetailsDAO iCodeDetailsDAO, String codeName) {
		CfgCodeDetails codeDetails = (CfgCodeDetails) iCodeDetailsDAO
				.getUniqueCodedetailsByCodeName(codeName);
		if (codeDetails != null) {
			ASCLogger.debug(this.getClass(), "addCode", "Add messageSource arg: " + codeDetails.getCodeDesc());
			addArgument(codeDetails.getCodeDesc());
		} else {
			throw new RuntimeException(
					"Unable to set code description because code is not found "
							+ "with code name " + codeName);
		}
	}
	
	public void addCode(String code, Object[]args) {
		addCode(code);
		for(Object arg: args) {
			addArgument(arg);
		}
	}
	
	public void setDefaultMessage(String defaultMessage) {
		this.defaultMessage = defaultMessage;
	}
	
	public ObjectError getObjectError() {
		String[] codes = codeList.toArray(new String[codeList.size()]);
		Object[] args = arguementList.toArray();
		return new ObjectError(objectName, codes, args, defaultMessage);
	}
	
	public ObjectError getObjectError(String defaultMessage) {
		String[] codes = codeList.toArray(new String[codeList.size()]);
		Object[] args = arguementList.toArray();
		return new ObjectError(objectName, codes, args, defaultMessage);
	}
}
